package com.coolweather.android;


import static com.coolweather.android.constants.Constants.AQI_URL;
import static com.coolweather.android.constants.Constants.KEY;
import static com.coolweather.android.constants.Constants.NOWWEATHER_URL;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import com.coolweather.android.databinding.ActivityMainBinding;
import com.coolweather.android.databinding.ActivityWeatherBinding;
import com.coolweather.android.gson.AQI;
import com.coolweather.android.gson.NowWeather;
import com.coolweather.android.util.HttpUtil;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class WeatherActivity extends AppCompatActivity {
    ActivityWeatherBinding binding;
    @Override
    protected void onCreate(Bundle saveInstanceState){
        super.onCreate(saveInstanceState);
        binding = ActivityWeatherBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        Intent intent = getIntent();


        String spot = intent.getStringExtra("spot");
        String location = intent.getStringExtra("location");
        Log.d("test2", "onCreate: "+spot+":"+location);

        showTitle(spot);
        requestWeather(location);
    }

    private void requestWeather(String location) {
        String url = String.format(NOWWEATHER_URL,location);
        Log.d("test", "request aqi: "+url);
        HttpUtil.sendOkHttpRequest(url, new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
            String strJson = response.body().string();
            NowWeather nowWeather = gson.fromJson(strJson,NowWeather.class);
            if (nowWeather.getCode().equals("200")){
                Log.d("test", String.format("当前气温：%s ℃，天气情况：%s",
                        nowWeather.getNow().getTemp(),
                        nowWeather.getNow().getText()));
                showNowWeather(nowWeather);
            }else{
                Log.d("test", "访问实时天气API出错 ");
            }
            }
        });

        String url_aqi = String.format(AQI_URL,location,KEY);
        Log.d("test", "request aqi: "+url_aqi);
        HttpUtil.sendOkHttpRequest(url_aqi, new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String strJson = response.body().string();
                AQI aqi = gson.fromJson(strJson,AQI.class);
                if (aqi.getCode().equals("200")){
                    Log.d("test", String.format("当前AQI：%s ℃，PM2.5：%s",
                            aqi.getNow().getAqi(),
                            aqi.getNow().getPm2p5()));
                    showAQI(aqi);
                }else{
                    Log.d("test", "访问实时天气API出错 ");
                }
            }
        });
    }

    private void showAQI(AQI aqi) {
        runOnUiThread(()->{
            binding.includeAqi.aqiText.setText(aqi.getNow().getAqi());
            binding.includeAqi.pm25Text.setText(aqi.getNow().getPm2p5());

        });
    }

    private void showNowWeather(NowWeather nowWeather) {
        runOnUiThread(()->{
            binding.includeNow.degreeText.setText(nowWeather.getNow().getTemp()+"℃");
            binding.includeNow.weatherInfoText.setText(nowWeather.getNow().getText());
        });
    }

    Gson gson = new Gson();

    private void showTitle(String spot) {
        binding.includeTitle.titleCity.setText(spot);
        SimpleDateFormat format = new SimpleDateFormat("yyy-MM-dd hh:mm:ss");
        binding.includeTitle.titleUpdateTime.setText(format.format(new Date()));
    }

}
